<template>
  <div class="app-container">
    <div class="search-bar">
      <el-form ref="queryFormRef" :model="queryParams" :inline="true">
        <el-form-item label="项目名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="项目名称"
            clearable
            @keyup.enter="handleQuery()"
          />
        </el-form-item>
        <el-form-item label="技术栈" prop="techs">
          <el-input-tag
            v-model="queryParams.techs"
            placeholder="请输入标签"
            aria-label="输入标签,回车键确认"
          />
        </el-form-item>
        <el-form-item label="项目时间" prop="date">
          <el-date-picker
            v-model="queryParams.date"
            class="!w-[240px]"
            type="date"
            placeholder="项目时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleQuery">
            <template #icon><Search /></template>
            搜索
          </el-button>
          <el-button @click="handleResetQuery">
            <template #icon><Refresh /></template>
            重置
          </el-button>
        </el-form-item>
      </el-form>
    </div>

    <el-card shadow="never">
      <div class="mb-10px">
        <el-button v-hasPerm="['system:project:add']" type="success" @click="handleOpenDialog()">
          <template #icon><Plus /></template>
          新增
        </el-button>
        <el-button
          v-hasPerm="['system:project:delete']"
          type="danger"
          :disabled="removeIds.length === 0"
          @click="handleDelete()"
        >
          <template #icon><Delete /></template>
          删除
        </el-button>
      </div>

      <el-table
        ref="dataTableRef"
        v-loading="loading"
        :data="pageData"
        highlight-current-row
        border
        @selection-change="handleSelectionChange"
      >
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column key="name" label="项目名称" prop="name" min-width="150" align="center" />
        <el-table-column label="技术栈">
          <template #default="{ row }">
            <el-tag v-for="(tech, index) in row.techs" :key="index" class="tech-tag">
              {{ tech }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column
          key="description"
          label="项目简介"
          prop="description"
          min-width="150"
          align="center"
        />
        <el-table-column key="repo" label="项目地址" prop="repo" min-width="150" align="center" />
        <el-table-column
          key="content"
          label="项目内容"
          prop="content"
          min-width="150"
          align="center"
        />
        <el-table-column key="date" label="项目时间" prop="date" min-width="150" align="center" />
        <el-table-column fixed="right" label="操作" width="220">
          <template #default="scope">
            <el-button
              v-hasPerm="['system:project:edit']"
              type="primary"
              size="small"
              link
              @click="handleOpenDialog(scope.row.id)"
            >
              <template #icon><Edit /></template>
              编辑
            </el-button>
            <el-button
              v-hasPerm="['system:project:delete']"
              type="danger"
              size="small"
              link
              @click="handleDelete(scope.row.id)"
            >
              <template #icon><Delete /></template>
              删除
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <pagination
        v-if="total > 0"
        v-model:total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="handleQuery()"
      />
    </el-card>

    <!-- 项目实践表单弹窗 -->
    <el-dialog
      v-model="dialog.visible"
      :title="dialog.title"
      width="500px"
      @close="handleCloseDialog"
    >
      <el-form ref="dataFormRef" :model="formData" :rules="rules" label-width="100px">
        <el-form-item label="项目名称" prop="name">
          <el-input v-model="formData.name" placeholder="项目名称" />
        </el-form-item>

        <el-form-item label="技术栈" prop="techs">
          <el-input-tag
            v-model="formData.techs"
            placeholder="请输入标签"
            aria-label="输入标签,回车键确认"
          />
        </el-form-item>

        <el-form-item label="项目简介" prop="description">
          <el-input v-model="formData.description" placeholder="项目简介" />
        </el-form-item>

        <el-form-item label="项目地址" prop="repo">
          <el-input v-model="formData.repo" placeholder="项目地址" />
        </el-form-item>

        <el-form-item label="项目内容" prop="content">
          <el-input v-model="formData.content" type="textarea" placeholder="项目内容" />
        </el-form-item>

        <el-form-item label="项目时间" prop="date">
          <el-date-picker
            v-model="formData.date"
            class="!w-[240px]"
            type="date"
            placeholder="项目时间"
            value-format="YYYY-MM-DD"
          />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="handleSubmit()">确定</el-button>
          <el-button @click="handleCloseDialog()">取消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
defineOptions({
  name: "Project",
  inheritAttrs: false,
});

import ProjectAPI, { ProjectPageVO, ProjectForm, ProjectPageQuery } from "@/api/system/project";

const queryFormRef = ref(ElForm);
const dataFormRef = ref(ElForm);

const loading = ref(false);
const removeIds = ref<number[]>([]);
const total = ref(0);

const queryParams = reactive<ProjectPageQuery>({
  pageNum: 1,
  pageSize: 10,
});

// 项目实践表格数据
const pageData = ref<ProjectPageVO[]>([]);

// 弹窗
const dialog = reactive({
  title: "",
  visible: false,
});

// 项目实践表单数据
const formData = reactive<ProjectForm>({});

// 项目实践表单校验规则
const rules = reactive({
  id: [{ required: true, message: "请输入项目ID", trigger: "blur" }],
  createdBy: [{ required: true, message: "请输入创建人ID", trigger: "blur" }],
  createdAt: [{ required: true, message: "请输入创建时间", trigger: "blur" }],
  updatedBy: [{ required: true, message: "请输入更新人ID", trigger: "blur" }],
  updatedAt: [{ required: true, message: "请输入更新时间", trigger: "blur" }],
});

/** 查询项目实践 */
function handleQuery() {
  loading.value = true;
  ProjectAPI.getPage(queryParams)
    .then((data) => {
      pageData.value = data.list;
      total.value = data.total;
    })
    .finally(() => {
      loading.value = false;
    });
}

/** 重置项目实践查询 */
function handleResetQuery() {
  queryFormRef.value!.resetFields();
  queryParams.pageNum = 1;
  handleQuery();
}

/** 行复选框选中记录选中ID集合 */
function handleSelectionChange(selection: any) {
  removeIds.value = selection.map((item: any) => item.id);
}

/** 打开项目实践弹窗 */
function handleOpenDialog(id?: number) {
  dialog.visible = true;
  if (id) {
    dialog.title = "修改项目实践";
    ProjectAPI.getFormData(id).then((data) => {
      Object.assign(formData, data);
    });
  } else {
    dialog.title = "新增项目实践";
  }
}

/** 提交项目实践表单 */
function handleSubmit() {
  dataFormRef.value.validate((valid: any) => {
    if (valid) {
      loading.value = true;
      const id = formData.id;
      if (id) {
        ProjectAPI.update(id, formData)
          .then(() => {
            ElMessage.success("修改成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      } else {
        ProjectAPI.add(formData)
          .then(() => {
            ElMessage.success("新增成功");
            handleCloseDialog();
            handleResetQuery();
          })
          .finally(() => (loading.value = false));
      }
    }
  });
}

/** 关闭项目实践弹窗 */
function handleCloseDialog() {
  dialog.visible = false;
  dataFormRef.value.resetFields();
  dataFormRef.value.clearValidate();
  formData.id = undefined;
}

/** 删除项目实践 */
function handleDelete(id?: number) {
  const ids = [id || removeIds.value].join(",");
  if (!ids) {
    ElMessage.warning("请勾选删除项");
    return;
  }

  ElMessageBox.confirm("确认删除已选中的数据项?", "警告", {
    confirmButtonText: "确定",
    cancelButtonText: "取消",
    type: "warning",
  }).then(
    () => {
      loading.value = true;
      ProjectAPI.deleteByIds(ids)
        .then(() => {
          ElMessage.success("删除成功");
          handleResetQuery();
        })
        .finally(() => (loading.value = false));
    },
    () => {
      ElMessage.info("已取消删除");
    }
  );
}

onMounted(() => {
  handleQuery();
});
</script>
